#ifdef MAC_OS_X
    #include "GDAL/gdal_priv.h"
#else
    #include "gdal_priv.h"
#endif

void print_base_info(GDALDataset * poDataset);
void print_band_info(GDALDataset * poDataset, int Band);

int main(int argc, char* argv[])
{
    GDALDataset  *poDataset;
    char* pszFilename = "./01.jpg";
    if (argc > 1) {
		pszFilename = argv[1];	
    }
    GDALAllRegister();
   
    poDataset = (GDALDataset *) GDALOpen( pszFilename, GA_ReadOnly );
    if( poDataset == NULL )
    {
        printf("NULL\n");
    }
    else {
        printf("---> GDAL open OK <---\n");
    }

    print_base_info(poDataset);
    printf("\n");
    
    print_band_info(poDataset, 1);
    print_band_info(poDataset, 2);
    print_band_info(poDataset, 3);

    GDALClose(poDataset);
}

int length(GByte * data) {
    int len = 0;
    for(; *data != '\0'; ++data) {
        ++len;
    }
    return len;
}

void print_band_info(GDALDataset *poDataset, int Band) {
    GDALRasterBand *poBand;
    int nBlockXSize, nBlockYSize;
    int bGotMin, bGotMax;
    double adfMinMax[2];

    poBand = poDataset->GetRasterBand(Band);
    poBand->GetBlockSize(&nBlockXSize, &nBlockYSize);
    printf("Block=%dx%d, Type=%s, ColorInterp=%s\n",
        nBlockXSize, nBlockYSize,
        GDALGetDataTypeName(poBand->GetRasterDataType()),
        GDALGetColorInterpretationName(poBand->GetColorInterpretation()));

    adfMinMax[0] = poBand->GetMinimum( &bGotMin );
    adfMinMax[1] = poBand->GetMaximum( &bGotMax );
    if( ! (bGotMin && bGotMax) )
        GDALComputeRasterMinMax((GDALRasterBandH)poBand, TRUE, adfMinMax);
    
    printf("Min=%.3fd, Max=%.3f\n", adfMinMax[0], adfMinMax[1]);

    if (poBand->GetOverviewCount() > 0)
        printf("Min=%.3fd, Max=%.3f\n", adfMinMax[0], adfMinMax[1]);

    if (poBand->GetColorTable() != NULL)
        printf("Band has a color table with %d entries.\n",
            poBand->GetColorTable()->GetColorEntryCount());
    
    GByte *pafScanline;
    int nXSize = poBand->GetXSize();
    int nYSize = poBand->GetYSize();

    pafScanline = (GByte *) CPLMalloc(nYSize*nXSize);
    poBand->RasterIO( GF_Read, 0, 0, nXSize, nYSize,
                      pafScanline, nXSize, nYSize, GDT_Byte,
                      0, 0);
    
    printf("nXSize: %d,nYSize: %d \n", nXSize, poBand->GetYSize());
    printf("image size: %d \n", length(pafScanline));
}

void print_base_info(GDALDataset * poDataset) {
    printf("Driver: %s/%s\n", 
            poDataset->GetDriver()->GetDescription(),
            poDataset->GetDriver()->GetMetadataItem( GDAL_DMD_LONGNAME ));

    printf("Size is %dx%dx%d\n",
            poDataset->GetRasterXSize(), poDataset->GetRasterYSize(),
            poDataset->GetRasterCount());
    
}
